import os
import threading

import pandas as pd
import tushare as ts
from loguru import logger

from models.stock_model import StockNumber, DayInfo
from mylib import download_all


def get_all_stock(fw, N, stock, end_date):
    df = pd.read_csv('../../all.csv')
    count = 0
    for row in df.index:
        if stock and df.loc[row]['ts_code'] != stock:
            continue
        sn = StockNumber(df.loc[row])
        if '退' in sn.name:
            continue
        if 'ST' in sn.name:
            continue
        if not str(sn.ts_code).startswith('60') \
                and not str(sn.ts_code).startswith('00'):
            continue
        count += 1
        analysis_stock(fw, N, sn, end_date)


def analysis_stock(fw, N, sn, end_date):
    csv_path = f'stocks/{sn.ts_code}.csv'
    if not os.path.exists(csv_path):
        pro = ts.pro_api(token='bbc6f076aa3d2b063cb26376ad12ffd94b53864b42d2344b3aa2039f')
        df2 = pro.daily(ts_code=sn.ts_code)
        save_dir = f'../../stocks'
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
        df2.to_csv(csv_path)
        print(f'save {sn.ts_code} {sn.name} 成功')
    df2 = pd.read_csv(csv_path)
    down_arr = []
    all_days_data = []
    today_di = None
    for row2 in df2.index:
        if len(down_arr) >= N:
            link_code_arr = sn.ts_code.split('.')
            link_code = f'{link_code_arr[1]}{link_code_arr[0]}'
            hyperlink = f'= HYPERLINK("https://xueqiu.com/S/{link_code}"),{di.name}'
            msg = f'{hyperlink},{di.industry},{down_arr[0].close},{down_arr[0].trade_date},{len(all_days_data)},天前{N}连跌\n'
            fw.write(msg)
            print(msg)
            all_days_data = []
            break
        di = DayInfo(sn, df2.loc[row2])
        di.name = sn.name
        di.ts_code = sn.ts_code
        di.industry = sn.industry
        # di.trade_date = df2.loc[row2]['trade_date']
        if str(di.trade_date) > str(end_date):
            continue
        # di.open = df2.loc[row2]['open']
        # di.high = df2.loc[row2]['high']
        # di.low = df2.loc[row2]['low']
        # di.close = float(df2.loc[row2]['close'])
        if di.close < 10:
            break
        # di.pre_close = df2.loc[row2]['pre_close']
        # di.change = df2.loc[row2]['change']
        # di.pct_chg = float(df2.loc[row2]['pct_chg'])
        # di.vol = df2.loc[row2]['vol']
        # di.amount = df2.loc[row2]['amount']
        if di.pct_chg < 0:
            down_arr.append(di)
        else:
            down_arr = []
        all_days_data.append(di)
        if today_di is None:
            today_di = di


def printNumber(stocks, file_path, N, end_date):
    with open(file_path, 'a+') as fw:
        title = "超链接,名字,行业,价格,不满足日期,ndays,描述\n"
        print(title)
        fw.write(title)
        for stock in stocks:
            get_all_stock(fw, N, stock, end_date)


def run():
    bkd = 3
    N = 2

    stocks = [
        '600438.SH',
    ]
    download_all.get_all_stock(stocks)

    log_dir = os.path.abspath(__file__).replace('.py', '')
    if not os.path.exists(log_dir):
        os.mkdir(log_dir)

    fpath = f'{log_dir}/0_n_down_bkd.txt'
    if os.path.exists(fpath):
        os.remove(fpath)
    logger.add(fpath)
    logger.info('生成csv start')

    df = pd.read_csv(f'../../stocks/{stocks[0]}.csv')
    all_trade_date_arr = []
    for row in df.index:
        all_trade_date_arr.append(df.loc[row]['trade_date'])

    file_path_arr = []
    for idx, trade_date in enumerate(all_trade_date_arr):
        if idx > bkd:
            break
        today_str = str(trade_date)
        file_name = '{}_连跌_{}.csv'.format(trade_date, N)
        file_path = f'{log_dir}/{file_name}'
        if os.path.exists(file_path):
            os.remove(file_path)

        file_path_arr.append((today_str, file_path))
        printNumber(stocks, file_path, N, trade_date, )

    logger.info('生成csv运行完成')


if __name__ == '__main__':
    run()
